import Context from "../../core/context/Context";
import { UIBase } from "../../core/gui/UIBase";
import { Logger } from "../../core/log/logger";
import TimeManage from "../../core/time/TimerManage";
import Utils from "../../core/utils/Utils";
import EventType from "../../module/common/EventType";
import UI_net_mask from "../../moduleui/common/UI_net_mask";

export default class NetMask extends UIBase<UI_net_mask> {

    /**背景半透明遮罩 */
    private mask: boolean = false;

    /**转圈动画延迟 */
    private dely: number = 200;

    /**自动关闭时间， -1不自动关闭*/
    private autoClose: number = -1;

    /**当前时间 */
    private currTime: number;

    private timer: TimeManage;

    protected createView(): UI_net_mask {
        return UI_net_mask.createInstance();
    }

    protected initialize(): void {
        this.view.sortingOrder = 999;
        this.timer = Context.Instance.get(TimeManage);
        this.view.setSize(Laya.stage.width, Laya.stage.height)
    }

    protected onOpen(d: any): void {

        d && this.updateData(d.mask, d.dely, d.autoClose);

        let view = this.view, dely = this.dely <= 0;

        view.m_tip.visible = this.mask;
        view.m_lod.visible = dely;

        Logger.logBusiness("打开遮罩");
    }

    protected onClose(reaskon?: string): void {
        Logger.logBusiness("关闭遮罩");
    }

    onLoopSecond() {
        // Logger.logBusiness(this.timer.ms, "当前时间：")
        if (this.autoClose < 0) {
            //不自动关闭
            return;
        }

        if (this.timer.ms >= this.currTime + this.autoClose) {
            this.close()
            return;
        }

        if (this.dely < 0) {
            return;
        }

        if (this.timer.ms >= this.currTime + this.dely) {
            this.dely = -1;
            this.view.m_lod.visible = true;
        }

    }

    updateData(mask: boolean, dely: number, autoClose: number) {
        !Utils.isNil(mask) && (this.mask = mask);
        !Utils.isNil(dely) && (this.dely = dely);
        !Utils.isNil(autoClose) && (this.autoClose = autoClose);
        this.currTime = this.timer.ms;
    }



}